package org.builder.excel;

import org.annotation.ExcelBean;
import org.builder.excelutil.IExcelExportUtil;
import org.util.FileUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * @author huangshen
 * @version 0
 * @description ExcelBuilder
 */
public abstract class BaseExcelExportBuild {
    private static final String CELL = "cell";
    /**
     * 最大行数
     */
    protected Integer maxRowNum = 65500;
    /**
     * 当前总行数
     */
    protected Integer totalRowNum = 0;
    /**
     * Excel标题
     */
    protected String excelTitle;
    /**
     * 导出路径
     */
    protected String exportAddress;
    /**
     * 导出工具类
     */
    protected IExcelExportUtil exportUtil;
    /**
     * 导出对象
     */
    protected Class<?> exportBean;
    /**
     * 导出数据集合
     */
    protected List<?> beanList;
    /**
     * 导出对象的get方法集合
     */
    protected List<Method> getMethodList;
    /**
     * 列名
     */
    protected List<String> columnNameList;

    protected List<Map<String,Object>> mapList;

    public <E> BaseExcelExportBuild init(String exportAddress, Class<E> exportBean, List<E> beanList) {
        this.exportBean = exportBean;
        this.exportAddress = exportAddress;
        this.beanList = beanList;
        this.excelTitle = this.exportBean.getAnnotation(ExcelBean.class).excelTitle();
        return this;
    }

    public <E> BaseExcelExportBuild init1(String exportAddress, Class<E> exportBean, List<Map<String, Object>> maps) {
        this.exportBean = exportBean;
        this.exportAddress = exportAddress;
        this.mapList = maps;
        this.excelTitle = this.exportBean.getAnnotation(ExcelBean.class).excelTitle();
        return this;
    }
    /**
     * 初始化
     */
    public abstract BaseExcelExportBuild prepared();

    public BaseExcelExportBuild initExcel() {
        exportUtil.initExcel();
        return this;
    }

    public BaseExcelExportBuild setTitle() {
        exportUtil.setTitle(excelTitle);
        return this;
    }

    public BaseExcelExportBuild setColName() {
        exportUtil.setColName(columnNameList);
        return this;
    }

    public BaseExcelExportBuild setCellStyle() {
        exportUtil.setCellStyle(CELL);
        return this;
    }

    /**
     * 设置数据
     */
    public abstract BaseExcelExportBuild setRowData();

    public void createExcel(OutputStream outputStream) {
        // 创建Excel
        exportUtil.createExcel(outputStream);
    }

    public void createExcel() {
        File file = new File(exportAddress);
        FileUtils.fileExits(file);
        try (OutputStream outputStream = new FileOutputStream(file)) {
            // 参数预处理 初始化
            prepared()
                // 初始化poi工具类
                .initExcel()
                // 设置标题
                .setTitle()
                // 设置列名
                .setColName()
                // 设置样式
                .setCellStyle()
                // 设置参数
                .setRowData()
                .createExcel(outputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
